package com.kang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kang.entity.Concern;
import com.kang.entity.Essay;
import com.kang.entity.Like;
import com.kang.mapper.EssayMapper;
import com.kang.mapper.LikeMapper;
import com.kang.service.ConcernService;
import com.kang.service.LikeService;
import com.kang.util.TokenToUserId;
import com.kang.vo.ParamInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private EssayMapper essayMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ConcernService concernService;

    @Override
    public Integer insertLike(HttpServletRequest request, Integer essayId) throws Exception {
        Integer userId = TokenToUserId.TokenGetUserId(request.getHeader("token"));
        Like like = new Like();
        like.setUserId(userId);
        like.setEssayId(essayId);
        like.setCreateTime(new Date());
        like.setUpdateTime(new Date());
        int insert = likeMapper.insert(like);
        if (insert == 1) {
            stringRedisTemplate.opsForValue().increment("user:like:" + essayId);
        }
        Set<String> keys = stringRedisTemplate.keys("user:like:".concat("*"));
        return insert;
    }

    @Override
    public Integer updateLike(HttpServletRequest request, Integer essayId) throws Exception {
        Integer userId = TokenToUserId.TokenGetUserId(request.getHeader("token"));
        QueryWrapper<Like> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId).eq("essay_id",essayId);
        int delete = likeMapper.delete(wrapper);
        if (delete == 1) {
            stringRedisTemplate.opsForValue().decrement("user:like:" + essayId);
        }
        return delete;
    }

    @Override
    public Map<String, Integer> getLikeCount(HttpServletRequest request) throws Exception {
        Map<String, Integer> map = new HashMap<>();
        Integer userId = TokenToUserId.TokenGetUserId(request.getHeader("token"));
        //统计所发布文章
        QueryWrapper<Essay> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Essay> essayList = essayMapper.selectList(wrapper);
        //根据文章查询被点赞数
        List<Integer> essayIdList = essayList.stream().map(Essay::getId).collect(Collectors.toList());
        QueryWrapper<Like> wrapper1 = new QueryWrapper<>();
        wrapper1.in("essay_id",essayIdList);
        Integer huoZhan = likeMapper.selectCount(wrapper1);
        map.put("huoZhan",huoZhan);
        ParamInfo paramInfo = new ParamInfo();

        paramInfo.setPageNum(1);
        paramInfo.setPageSize(10);
        Page<Concern> guanZhu = concernService.getConcernCount(paramInfo);
        map.put("guanZhu", (int) guanZhu.getTotal());
        Page<Concern> fenSiCount = concernService.getFenSiCount(paramInfo);
        map.put("fenShi", (int) fenSiCount.getTotal());
        return map;
    }
}
